package com.dashan.servlets;

import static com.dashan.Constants.FRIEND_REQUEST_MESSAGE;
import static com.dashan.Constants.FRIEND_REQUEST_TO;
import static com.dashan.Constants.SUCCESS;
import static com.dashan.Constants.USERNAME;
import static com.dashan.servlets.Parameter.Option.Required;

import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.jdo.JDOObjectNotFoundException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dashan.backend.DashanException;
import com.dashan.backend.FriendManager;

@SuppressWarnings("serial")
public class InviteFriendServlet extends DashanPrivateServlet {

    @Override
    public ArrayList<Parameter> getParameterList() {
        ArrayList<Parameter> res = super.getParameterList();
        res.add(new Parameter(FRIEND_REQUEST_TO, Required, String.class));
        res.add(new Parameter(FRIEND_REQUEST_MESSAGE, Required, String.class));
        return res;
    }

    @Override
    public String getUrlPattern() {
        return "/invitefriendservlet";
    }
    
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException ,IOException {
        TreeMap<String, Parameter> params = loadParamters(req);
        
        try {
            FriendManager.inviteFriend(params.get(USERNAME).stringValue(),
                    params.get(FRIEND_REQUEST_TO).stringValue(),
                            params.get(FRIEND_REQUEST_MESSAGE).stringValue());
        } catch (JDOObjectNotFoundException e) {
            throw new ServletException(e.getMessage());
        } catch (DashanException e) {
            throw new ServletException(e.getMessage());
        }
        
        resp.getWriter().print(SUCCESS);
    };
}
